from selenium.webdriver.common.by import By
from Utils import TestDriver


class TestLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.driver = TestDriver.driver
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver.get(self.url)
        self.driver.fullscreen_window()

    def LoginSuccess(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        #输入框输入账号
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("xiaohua")
        #输入框输入密码
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        #点击登陆
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        #进入问卷首页后,验证头像,查看是不是进入了首页
        self.driver.find_element(By.CSS_SELECTOR,"#sk-layout > div > div > section > div > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost.ant-page-header-compact > div.ant-page-header-content > div > div > div > div > div > div.avatar___3ZBop > span > img")
        #添加截图
        TestDriver.getScreenShot()

    def LoginFail(self):
        for i in range(0,2):
            #验证失败账号登陆之后还要错误的密码登陆,要清空输入框
            self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
            self.driver.find_element(By.CSS_SELECTOR,"#password").clear()

            if i == 0:
                #输入框输入正确的账号,错误的密码
                self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("xiaohua")
                self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
            else:
                #输入框输入错误的账号,正确的密码
                self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("xiao")
                self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")

            #点击登陆
            self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()

            #验证是不是还在登陆界面,显示错误的用户名和密码
            text = self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
            assert(text == '错误的用户名和密码')

            #添加截图
            TestDriver.getScreenShot()